﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Imaging;
using System.IO;

namespace LibraryCS {
    /// <summary>
    /// Class for creating a fontset as images
    /// creates the characters 0 - 9, a - z, A-Z
    /// </summary>
    public class FontSet {
        private char[] characters = new char[62] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
            'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
            'U', 'V', 'W', 'X', 'Y', 'Z'};

        private PrivateFontCollection pfc;
        private Font font;
        public String Name;

        private Bitmap[] bitmaps = new Bitmap[62];

        /// <summary>
        /// Constructor initializing the input font file
        /// </summary>
        /// <param name="path">Path + filename </param>
        /// <param name="name">Nametag for this class. Also used for the output directoryname</param>
        public FontSet(string path, string name) {
            pfc = new PrivateFontCollection();
            pfc.AddFontFile(path);
            Name = name;

            if (pfc.Families[0].IsStyleAvailable(FontStyle.Regular)) {
                font = new Font(pfc.Families[0], 100, FontStyle.Regular, GraphicsUnit.Pixel);
            } else if (pfc.Families[0].IsStyleAvailable(FontStyle.Bold)) {
                font = new Font(pfc.Families[0], 100, FontStyle.Bold, GraphicsUnit.Pixel);
            } else if (pfc.Families[0].IsStyleAvailable(FontStyle.Italic)) {
                font = new Font(pfc.Families[0], 100, FontStyle.Italic, GraphicsUnit.Pixel);
            } else {
                Console.Out.WriteLine("No FontStyle available");
            }
        }

        /// <summary>
        /// creates the bitmaps of the font file in the directory:
        /// path + name
        /// </summary>
        /// <returns>A list of VImage objects of all the font bitmaps made</returns>
        public List<VImage> createBitmaps() {
            List<VImage> images = new List<VImage>();
            for (int i = 0; i < bitmaps.Length; i++) {
                bitmaps[i] = new Bitmap(160, 160, PixelFormat.Format32bppRgb);

                Graphics g = Graphics.FromImage(bitmaps[i]);
                g.Clear(Color.White);
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                g.DrawString("" + characters[i], font, new SolidBrush(Color.Black), 10, 0);
                g.Dispose();

                VImage vImage = new VImage(bitmaps[i]);

                // Crop the image
                VImage result = vImage.BoundingCrop();

                // Scale the image
                result = result.Scale(100, 150);

                // Save templates to disk (Debugging)
                // Create directory
                if (!Directory.Exists(TemplatePreparation.templateDir + Name)) {
                    Directory.CreateDirectory(TemplatePreparation.templateDir + Name);
                }
                if (characters[i] < 65) {
                    result.localBitmap.Save(TemplatePreparation.templateDir + Name + "\\" + characters[i] + ".jpg", ImageFormat.Png);
                } else if (characters[i] > 64 && characters[i] < 91) {
                    result.localBitmap.Save(TemplatePreparation.templateDir + Name + "\\" + characters[i] + "_upper.jpg", ImageFormat.Png);
                } else {
                    result.localBitmap.Save(TemplatePreparation.templateDir + Name + "\\" + characters[i] + "_lower.jpg", ImageFormat.Png);
                }

                result.localBitmap.Tag = characters[i];
                images.Add(result);
            }
            return images;
        }
    }
}
